
local ClientOHOH = _G.ClientOHOH
local M = ClientOHOH.class.new(script,ClientOHOH.files.BaseObject(),"PetGoldGui_"..script.Name)
local CallBackManager = ClientOHOH.load.CallBackManager();
local Good_Cfg=ClientOHOH.config.Goods
local ui_list=ClientOHOH.load.UIManager().ui_list
local LanguageHelper=_G.LanguageHelper 
local GlobalTipGui=ui_list.GlobalTipGui
local PetTool =  _G.CommonOHOH.load.PetTool()
local probability = nil
local buyData={
    gid = 13
}
local selectedCount = 0
local SelectedId = 0
local initStr = {
    buy = "ctrl.craft.buy",
    make = "ctrl.craft.make",
    percent = "ctrl.craft.num"
}


function M.new(...)
    return M(...)
end

function M.ctor(self,ctrl)
    M.super.ctor(self)
    self.ctrl = ctrl
    self.player = _G.client.local_player
    self.ui = ctrl:InitUiInst2(ctrl.main_inst,initStr)
    buyData.gType = Good_Cfg[buyData.gid].gType
    CallBackManager:AddRbxConnect(self,self.ui.buy.Activated,function()
        self.ctrl.BuyEvent:Action(buyData)
    end)
    CallBackManager:AddRbxConnect(self,self.ui.make.Activated,function()
        if selectedCount == 0 then
                GlobalTipGui:Show(LanguageHelper:GetText(999006))
            return 
        end
        local isSuccess = self.ctrl.MakeEvent:Action(SelectedId,selectedCount)
        if isSuccess[1] then
            GlobalTipGui:Show(LanguageHelper:GetText(999004))
        else
            GlobalTipGui:Show(LanguageHelper:GetText(999005))
        end
    end)
    self:AddConnect()
end

function M.AddConnect(self)
    self.ctrl.DatabaseReadyEvent:Connect(function()
        self:UpdateUi()
        self:CheckBuyDone()
    end)

    --点击后更新概率
    self.ctrl.PetSelectedEvent:Connect(function(count,petId)
        SelectedId = petId
        self:UpdateCount(count)
        self:UpdateUi()
    end)

    self.ctrl.gamepassBuyDoneEvent:Connect(function()
        self:CheckBuyDone()
    end)
end
--更新概率
function M.UpdateUi(self)
    self:CalcuProbability()
    self.ui.percent.Text = self.totalProbability.."%"
end

--检查是否购买
function M.CheckBuyDone(self)
    local gamepssList = self.ctrl:GetDataBase():GetData("gamePass")
    for index, _ in pairs(gamepssList) do
        if index == "gamepass_make_gold" then
            self.ui.buy.Visible = false
            self:UpdateUi()
        end
    end
end

--计算概率
function M.CalcuProbability(self)
    if probability==nil then
        probability = 0
    end
    local totalProbability = selectedCount*probability
    if totalProbability == 100 then
        self.ctrl.isHundredPercentEvent:Action(true)
    else
        self.ctrl.isHundredPercentEvent:Action(false)
    end
    self.totalProbability = totalProbability
end

--更新选中个数
function M.UpdateCount(self,count)
    selectedCount = count
end

function M.SetProbability(prob)
    probability = prob
end

function M.dtor(self)
	M.super.dtor(self);
end;


return M